package com.qf.agro.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.qf.agro.common.JsonObjectMapper;
import com.qf.agro.interceptors.CheckLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @company：千峰教育
 * @author：zhy
 * @date：2023/2/15
 */
@Configuration
@EnableSwagger2//开启Swagger
@EnableKnife4j//开启Knife4j
public class AgroWebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private CheckLoginInterceptor checkLoginInterceptor;
//
//    @Autowired
//    private PermissionInterceptor permissionInterceptor;

    /**
     * 指定静态资源位置
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * 指定查找静态资源的位置
         */
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");

        /*文档需要映射的静态资源*/
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    public Docket createRestApi() {
        // 配置生成的文档信息
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder().title("智慧农业接口文档").version("1.0").description("智慧农业接口文档").build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qf.agro.controller"))
                .paths(PathSelectors.any())
                .build();
    }


    /**
     * 手动添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //创建不用拦截的路径数组                                                                       //文档需要释放的静态资源
        String[] pathArray = new String[]{
                                "/backend/**",
                                "/sys/user/login",
                                "/sys/user/logout",
                                "/error",
                                "/device/dataLogs/export",
                                "/doc.html",
                                "/webjars/**",
                                "/swagger-resources",
                                "/v2/api-docs"};
        /*
         * 注册拦截器
         *
         *  .addPathPatterns("/**") 指定拦截的规则
         * .excludePathPatterns(pathArray) 指定排除的路径
         */
        registry.addInterceptor(checkLoginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(pathArray);
//        registry.addInterceptor(permissionInterceptor)
//                .addPathPatterns("/**")
//                .excludePathPatterns("/sys/module/menu")
//                .excludePathPatterns(pathArray);
    }


    @Autowired
    private JsonObjectMapper jsonObjectMapper;

    //扩展mvc框架的消息转换器
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建消息转换器对象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //设置对象转换器，底层使用Jackson将Java对象转为json
        messageConverter.setObjectMapper(jsonObjectMapper);
        //将上面的消息转换器对象追加到mvc框架的转换器集合中
        converters.add(0, messageConverter);
    }
}
